package view;

import javax.swing.JDialog;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controller.ContactsControllerListener;
import controller.JTextFieldListener;
import po.*;

/**
 * 添加宿舍对话框
 * @author 27261
 *
 */
public class RoomDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener contactsControllerListener;
	private JTextField idTextField,nameTextField;
	private JButton confirmButton;
	private JPanel roomAddPanel;
	private Room room;
	public RoomDialog(ContactsFrame owner,String title,Room room) {
		this.contactsControllerListener = owner.getControllerListener();  //让RoomFrame和Dialog共用同一个controller对象
		this.contactsControllerListener.setRoomAddDialog(this);
		this.setTitle(title);
		this.room = room;
		this.setResizable(false);
		this.add(getRoomAddPanel());
		this.setModal(true);
		this.setSize(250, 150);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	public RoomDialog(OverviewDialog owner,String title,Room room) {
		this.contactsControllerListener = owner.getControllerListener();  //让RoomFrame和Dialog共用同一个controller对象
		this.contactsControllerListener.setRoomAddDialog(this);
		this.setTitle(title);
		this.room = room;
		this.setResizable(false);
		this.add(getRoomAddPanel());
		this.setModal(true);
		this.setSize(250, 150);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	public void closeCurrentDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	/**
	 * 添加宿舍信息表单面板
	 * @return
	 */
	private JPanel getRoomAddPanel() {
		roomAddPanel = new JPanel();
		if("添加宿舍信息".equals(this.getTitle())) {
			nameTextField = new JTextField(12);
			idTextField = new JTextField(12);
			confirmButton = new JButton("确定添加");
			confirmButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
			confirmButton.setPreferredSize(new Dimension(100, 30));
			confirmButton.setActionCommand("roomAddPost"); //添加宿舍信息
			nameTextField.addFocusListener(new JTextFieldListener(nameTextField, "例如：泰山区11-101"));
			idTextField.addFocusListener(new JTextFieldListener(idTextField, "例如：1"));
		}else {
			idTextField = new JTextField(String.valueOf(room.getRoom_id()),12);
			nameTextField = new JTextField(room.getRoom_name(),12);
			confirmButton = new JButton("确定修改");
			confirmButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
			confirmButton.setPreferredSize(new Dimension(100, 30));
			confirmButton.setActionCommand("updateRoomPost"); //修改宿舍信息
		}
		confirmButton.addActionListener(contactsControllerListener);
		JLabel a = new JLabel("大学宿舍号:");
		JLabel b = new JLabel("宿    舍    名:");
		a.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		b.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		roomAddPanel.add(a);
		roomAddPanel.add(idTextField);
		roomAddPanel.add(b);
		roomAddPanel.add(nameTextField);
		roomAddPanel.add(confirmButton);
		return roomAddPanel;
	}
	public JTextField getIdTextField() {
		return idTextField;
	}
	public JTextField getNameTextField() {
		return nameTextField;
	}
	public JButton getConfirmButton() {
		return confirmButton;
	}
	public ContactsControllerListener getControllerListener() {
		return contactsControllerListener;
	}
}
